/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import common.Codes;
import common.Envelope;
import common.FileReceiver;
import common.FileSender;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import common.Framer;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mateusz
 */
public class SendingProtocol {

    private Socket socket;
    private OutputStream out;
    private InputStream in;
    private Framer framer;
    private static final Logger LOGGER = Logger.getLogger(SendingProtocol.class.getName());

    public void init(String address, int port) throws UnknownHostException, IOException {
        socket = new Socket(address, port);
        out = socket.getOutputStream();
        in = socket.getInputStream();
        framer = new Framer(socket);
        LOGGER.log(Level.INFO, "Sending protocol initiated for address {0}", socket.getInetAddress().getHostAddress());
    }

    public boolean hello() {
        LOGGER.log(Level.INFO, "Sending hello...");
        framer.sendCode(0);
        Envelope env = framer.get();
        int code = env.getCode();
        LOGGER.log(Level.INFO, Codes.get(code));
        return code == 0;
    }

    public boolean newFolder(String name) {
        LOGGER.log(Level.INFO, "Sending new folder {0} ...", name);
        framer.sendMessage(11, name);
        Envelope env = framer.get();
        LOGGER.log(Level.INFO, Codes.get(env.getCode()));
        return framer.get().getCode() == 19;
    }

    public boolean newFile(File file) {
        LOGGER.log(Level.INFO, "Sending new file {0} ", file.getName());
        framer.sendFile(12, file);
        if (framer.get().getCode() != 19) {
            return false;
        }
        FileSender sender = new FileSender();
        sender.start(socket, file);
        Envelope env = framer.get();
        LOGGER.log(Level.INFO, Codes.get(env.getCode()));
        if (env.getCode() != 19) {
            return false;
        } else {
            return true;
        }
    }

    public void backFolder() {
        LOGGER.log(Level.INFO, "Sending back folder ");
        framer.sendCode(13);
    }

    public void sendTestClassPath(String path) {
        LOGGER.log(Level.INFO, "Sending test classpath {0} ", path);
        framer.sendMessage(11, path);
    }

    public void end() {
        LOGGER.log(Level.INFO, "Sending end sending data");
        framer.sendCode(15);
    }

    public void goodbye() {
        LOGGER.log(Level.INFO, "Sending goodbye");
        framer.sendCode(3);
    }

    public boolean sendAnswerRequest(File file) {
        LOGGER.log(Level.INFO, "Sending amswer request");
        framer.sendCode(2);
        Envelope env = framer.get();
        LOGGER.log(Level.INFO, Codes.get(env.getCode()));
        if (env.getCode() != 21) {
            return false;
        }
        LOGGER.log(Level.INFO, "Saving to file {0}", file.getName());
        FileReceiver frec = new FileReceiver();
        frec.start(socket, file);
        LOGGER.log(Level.INFO, "Saved");
        return true;
    }
}
